// request.js
const baseUrl = 'https://110.191.238.10:8050'; // 你的 API 基础 URL

function request(options) {
	const {
		url,
		method = 'GET',
		data = {},
		headers = {
			Authorization: uni.getStorageSync('token'),// 获取本地缓存的token
		}
	} = options;
	uni.showLoading({
		title:'请稍后...'
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method,
			data,
			header: headers,
			success: (response) => {
				if (response.data.code == 200 && response.data.code < 300) {
					resolve(response.data);
				}else if(response.data.code==401) {
					uni.clearStorageSync()
					uni.reLaunch({
						url:'/pages/login/login'
					})
				}else {
					uni.showToast({
						title: response.data.msg||'请求失败',
						icon: 'none'
					});				
					reject(new Error('Network response error'));
				}
				uni.hideLoading()
			},
			fail: (error) => {
				reject(error);
				uni.hideLoading()
			}
		});
	});
}

// 导出方法
export default request;